package com.ptu.server.Impl;

import com.ptu.dto.CartDTO;
import com.ptu.dto.UserDTO;
import com.ptu.entity.Cart;
import com.ptu.mapper.CartMapper;
import com.ptu.mapper.ProductMapper;
import com.ptu.server.CartServer;
import com.ptu.util.UserHolder;
import com.ptu.vo.CartVO;
import com.ptu.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CartServerImpl implements CartServer {



    @Autowired
    CartMapper cartMapper;

    @Autowired
    ProductMapper productMapper;


    @Override
    public void addCart(int productId) {

        //从UserHolder中获取用户id
        UserDTO user = UserHolder.getUser();
        int userId = user.getId();

        Cart cartById = cartMapper.getById(userId,productId);

        if (cartById == null) {
            ProductVO detailById = productMapper.getDetailById(productId);
            Cart cart = new Cart();
            cart.setUserId(userId);
            cart.setProductId(productId);
            cart.setQuantity(1);
            cart.setUnitPrice(detailById.getPrice());
            //把当前时间设置到数据库中
            cart.setCreateTime(new Timestamp(System.currentTimeMillis()));
            cart.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            cartMapper.insert(cart);
            System.out.println("添加购物车成功");
        }else {
            //数量加1,时间更新
            cartMapper.addQuantity(productId, LocalDateTime.now());
            System.out.println("数量加1");
        }

    }



    @Override
    public List<CartVO> getCartList() {

        //从UserHolder中获取用户id
        UserDTO user = UserHolder.getUser();
        int userId = user.getId();

        List<CartVO> cartList = cartMapper.getCartList(userId);

        //获取商品信息
        for (CartVO cartVO : cartList) {
            ProductVO detailById = productMapper.getDetailById(cartVO.getProductId());
            cartVO.setProductName(detailById.getName());
            cartVO.setProductImage(detailById.getImage());
        }

        return cartList;
    }

    @Override
    public void deleteCart(Integer productId) {

        //从UserHolder中获取用户id
        UserDTO user = UserHolder.getUser();
        int userId = user.getId();

        cartMapper.deleteCart(userId, productId);
        System.out.println("删除购物车成功");
    }

    @Override
    public void clearCart() {

        //从UserHolder中获取用户id
        UserDTO user = UserHolder.getUser();
        int userId = user.getId();

        cartMapper.clearCart(userId);
    }

    @Override
    public void subtractQuantity(Integer productId) {

        //从UserHolder中获取用户id
        UserDTO user = UserHolder.getUser();
        int userId = user.getId();

        Cart cartById = cartMapper.getById(userId, productId);

        if (cartById.getQuantity() == 1) {
            cartMapper.subtractCart(userId, productId);
            return;
        }

        //更新时间
        cartMapper.subtractQuantity(userId, productId, LocalDateTime.now());
    }

    //批量删除
    @Override
    @Transactional
    public void batchDelete(List<Integer> Ids) {
        //从UserHolder中获取用户id
        UserDTO user = UserHolder.getUser();
        int userId = user.getId();
        System.out.println("用户id: " + userId);
        System.out.println("待删除商品id: " + Ids);

        cartMapper.batchDelete(Ids, userId);

//        //循环删除
//        for (Integer productId : productIds) {
//            cartMapper.deleteCart(userId, productId);
//        }


        System.out.println("Server批量删除成功");
    }

}
